﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FWebLinkWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FWebLinkWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "Liên kết Web");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("ViewRight",GetText("ViewRight","ViewRight"),0),             
                };
            }
        }

        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("ViewRight"))
                {
                    return ClientRenderWebPartRight(TabModuleId, HasPorlet);
                }
                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }



        private string ClientRenderWebPartRight(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            string ActiveLanguage = Environments.ActiveLanguage;
            DataTable dtItems = new WebLinkBLL().ReadingItems(TabModuleCls.Id);
            if (dtItems.Rows.Count == 0) return "";
            StringBuilder Html = new StringBuilder();
            Html.Append(
                "<div id=\"weblink\">				\n" +
                "				<select class=\"weblink\" onchange=\"javascript:window.open(this.value);\" name=\"Web_link\" id=\"Web_link\" style=\"margin: 0pt; width: 100%;\">\n" +
                "<option value = 0 >- - -" + GetText("WebLinks", "Liên kết nhanh") + "- - -</option>");
            for (int index = 0; index < dtItems.Rows.Count; index++)
            {
                string WebName = "";
                try
                {
                    WebName = (string)dtItems.Rows[index][ActiveLanguage + "GroupLinkItemName"];
                }
                catch { }
                string WebUrl = "";
                try
                {
                    WebUrl = (string)dtItems.Rows[index]["Url"];
                }
                catch { }
                Html.Append(
                    " <option value=\"" + WebUrl + "\">" + WebName + "</option> \n"
                    );
            }
            Html.Append(
                "</select>" +
                "</div>");
            return Html.ToString();
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FWebLinkWP";
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new WebLinkBLL().ReadingForService(belongSite, ActiveLanguage);
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
